const types = require("../types");

const beforeParse = function ({ html }) {
  html = html.replace(
    '<meta http-equiv="content-type" content="text/html;charset=utf-8" />',
    ""
  );
  return (
    "<!DOCTYPE html>" +
    html.substring(html.indexOf("<html>"), html.indexOf("</html>") + 7)
  );
};

const beforeGenerateToc = function ({
  $,
  relativePath,
  addDashAnchor,
  docset,
}) {
  $("head script").each((index, element) => {
    if (element.attribs.src.indexOf("jquery-2.0.3.min65af.js") === -1) {
      $(element).remove();
    }
  });
  $("body script").remove();
  $(".site-header").remove();
  $(".site-footer").remove();
  $("body").append("<script src='../js/commands48d9.js?1426205833'></script>");
};

const generateToc = function ({
  $,
  relativePath,
  addDashAnchor,
  insertToDb,
  docset,
}) {
  let h1Title = $("h1");
  if (h1Title.text() === "404 - 悲剧了吧" || relativePath.endsWith("-2.html")) {
    return;
  }
  if (
    relativePath.indexOf("topics") === -1 &&
    !relativePath.endsWith("documentation.html")
  ) {
    return;
  }
  insertToDb({
    name: h1Title.length > 0 ? h1Title.text().trim() : $("h2").text().trim(),
    type: types.Guide,
    path: relativePath,
  });
};

const beforeFilter = function ({ $, relativePath, addDashAnchor, docset }) {};

const filter = function ({ $, relativePath, addDashAnchor, docset }) {};

const afterFilter = function ({ $, relativePath, addDashAnchor, docset }) {};

module.exports = {
  name: "redis",
  displayName: "Redis",
  platformFamily: "Redis",
  entry: "www.redis.cn/documentation.html",
  domain: "www.redis.cn",
  include: [],
  exclude: [],
  indexGenerator: [
    {
      file: "documentation.html",
      generateToc: function ({ insertToDb }) {
        let commands = [
          "APPEND",
          "AUTH",
          "BGREWRITEAOF",
          "BGSAVE",
          "BITCOUNT",
          "BITFIELD",
          "BITOP",
          "BITPOS",
          "BLPOP",
          "BRPOP",
          "BRPOPLPUSH",
          "BZPOPMIN",
          "BZPOPMAX",
          "CLIENT ID",
          "CLIENT KILL",
          "CLIENT LIST",
          "CLIENT GETNAME",
          "CLIENT PAUSE",
          "CLIENT REPLY",
          "CLIENT SETNAME",
          "CLIENT UNBLOCK",
          "CLUSTER ADDSLOTS",
          "CLUSTER COUNT-FAILURE-REPORTS",
          "CLUSTER COUNTKEYSINSLOT",
          "CLUSTER DELSLOTS",
          "CLUSTER FAILOVER",
          "CLUSTER FORGET",
          "CLUSTER GETKEYSINSLOT",
          "CLUSTER INFO",
          "CLUSTER KEYSLOT",
          "CLUSTER MEET",
          "CLUSTER NODES",
          "CLUSTER REPLICATE",
          "CLUSTER RESET",
          "CLUSTER SAVECONFIG",
          "CLUSTER SET-CONFIG-EPOCH",
          "CLUSTER SETSLOT",
          "CLUSTER SLAVES",
          "CLUSTER REPLICAS",
          "CLUSTER SLOTS",
          "COMMAND",
          "COMMAND COUNT",
          "COMMAND GETKEYS",
          "COMMAND INFO",
          "CONFIG GET",
          "CONFIG REWRITE",
          "CONFIG SET",
          "CONFIG RESETSTAT",
          "DBSIZE",
          "DEBUG OBJECT",
          "DEBUG SEGFAULT",
          "DECR",
          "DECRBY",
          "DEL",
          "DISCARD",
          "DUMP",
          "ECHO",
          "EVAL",
          "EVALSHA",
          "EXEC",
          "EXISTS",
          "EXPIRE",
          "EXPIREAT",
          "FLUSHALL",
          "FLUSHDB",
          "GEOADD",
          "GEOHASH",
          "GEOPOS",
          "GEODIST",
          "GEORADIUS",
          "GEORADIUSBYMEMBER",
          "GET",
          "GETBIT",
          "GETRANGE",
          "GETSET",
          "HDEL",
          "HEXISTS",
          "HGET",
          "HGETALL",
          "HINCRBY",
          "HINCRBYFLOAT",
          "HKEYS",
          "HLEN",
          "HMGET",
          "HMSET",
          "HSET",
          "HSETNX",
          "HSTRLEN",
          "HVALS",
          "INCR",
          "INCRBY",
          "INCRBYFLOAT",
          "INFO",
          "KEYS",
          "LASTSAVE",
          "LINDEX",
          "LINSERT",
          "LLEN",
          "LPOP",
          "LPUSH",
          "LPUSHX",
          "LRANGE",
          "LREM",
          "LSET",
          "LTRIM",
          "MEMORY DOCTOR",
          "MEMORY HELP",
          "MEMORY MALLOC-STATS",
          "MEMORY PURGE",
          "MEMORY STATS",
          "MEMORY USAGE",
          "MGET",
          "MIGRATE",
          "MONITOR",
          "MOVE",
          "MSET",
          "MSETNX",
          "MULTI",
          "OBJECT",
          "PERSIST",
          "PEXPIRE",
          "PEXPIREAT",
          "PFADD",
          "PFCOUNT",
          "PFMERGE",
          "PING",
          "PSETEX",
          "PSUBSCRIBE",
          "PUBSUB",
          "PTTL",
          "PUBLISH",
          "PUNSUBSCRIBE",
          "QUIT",
          "RANDOMKEY",
          "READONLY",
          "READWRITE",
          "RENAME",
          "RENAMENX",
          "RESTORE",
          "ROLE",
          "RPOP",
          "RPOPLPUSH",
          "RPUSH",
          "RPUSHX",
          "SADD",
          "SAVE",
          "SCARD",
          "SCRIPT DEBUG",
          "SCRIPT EXISTS",
          "SCRIPT FLUSH",
          "SCRIPT KILL",
          "SCRIPT LOAD",
          "SDIFF",
          "SDIFFSTORE",
          "SELECT",
          "SET",
          "SETBIT",
          "SETEX",
          "SETNX",
          "SETRANGE",
          "SHUTDOWN",
          "SINTER",
          "SINTERSTORE",
          "SISMEMBER",
          "SLAVEOF",
          "REPLICAOF",
          "SLOWLOG",
          "SMEMBERS",
          "SMOVE",
          "SORT",
          "SPOP",
          "SRANDMEMBER",
          "SREM",
          "STRLEN",
          "SUBSCRIBE",
          "SUNION",
          "SUNIONSTORE",
          "SWAPDB",
          "SYNC",
          "TIME",
          "TOUCH",
          "TTL",
          "TYPE",
          "UNSUBSCRIBE",
          "UNLINK",
          "UNWATCH",
          "WAIT",
          "WATCH",
          "ZADD",
          "ZCARD",
          "ZCOUNT",
          "ZINCRBY",
          "ZINTERSTORE",
          "ZLEXCOUNT",
          "ZPOPMAX",
          "ZPOPMIN",
          "ZRANGE",
          "ZRANGEBYLEX",
          "ZREVRANGEBYLEX",
          "ZRANGEBYSCORE",
          "ZRANK",
          "ZREM",
          "ZREMRANGEBYLEX",
          "ZREMRANGEBYRANK",
          "ZREMRANGEBYSCORE",
          "ZREVRANGE",
          "ZREVRANGEBYSCORE",
          "ZREVRANK",
          "ZSCORE",
          "ZUNIONSTORE",
          "SCAN",
          "SSCAN",
          "HSCAN",
          "ZSCAN",
          "XINFO",
          "XADD",
          "XTRIM",
          "XDEL",
          "XRANGE",
          "XREVRANGE",
          "XLEN",
          "XREAD",
          "XGROUP",
          "XREADGROUP",
          "XACK",
          "XCLAIM",
          "XPENDING",
        ];
        for (const command of commands) {
          insertToDb({
            name: command,
            type: types.Command,
            path: `www.redis.cn/commands/${command
              .toLocaleUpperCase()
              .replace(" ", "-")}.html`,
          });
        }
      },
    },
  ],
  generator: [
    {
      pattern: /.*/g,
      beforeParse,
      beforeGenerateToc,
      generateToc,
      filter,
      beforeFilter,
      afterFilter,
    },
  ],
};
